package com.Khan.SocialRescueSys.controller;

import com.Khan.SocialRescueSys.pojo.Comment;
import com.Khan.SocialRescueSys.pojo.User;
import com.Khan.SocialRescueSys.service.CommentService;
import com.Khan.SocialRescueSys.service.ForceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpSession;

/**
 * @author Khan Liu
 * @date 2022/4/5 15:16
 */
@Controller
public class CommentController {

    @Autowired
    private CommentService commentService;

    @Autowired
    private ForceService forceService;


    @GetMapping("/comments/{forceId}")  //展示留言
    public String comments(@PathVariable Long forceId, Model model){
        model.addAttribute("comments", commentService.getCommentByForceId(forceId));
        model.addAttribute("force", forceService.getDetailedForce(forceId));
        return "detail :: commentList";
    }

    @PostMapping("/comments")   //提交留言
    public String post(Comment comment){
        Long forceId = comment.getForce().getId();
        comment.setForce(forceService.getDetailedForce(forceId));  //绑定博客与评论
        comment.setForceId(forceId);
        System.out.println(comment);
        String content = comment.getContent();
        String email = comment.getEmail();
        if(content.length() > 255){
            comment.setContent(content.substring(0,255));
        }
        if(content==null || content.equals("")){
            return "redirect:/comments/" + forceId;
        }
        if(comment.getNickname() == null || comment.getNickname().equals("")){
            return "redirect:/comments/" + forceId;
        }
        if(email == null || email.equals("")){
            return "redirect:/comments/" + forceId;
        }
        if(!email.contains("@")){
            return "redirect:/comments/" + forceId;
        }
        commentService.saveComment(comment);
        return "redirect:/comments/" + forceId;
    }
}
